#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnDebugTime_clicked()
{
    QTime TM1(13, 24, 15);
    QString str = TM1.toString("HH:mm:ss");
    //qDebug() << "AAA" << Qt::endl;
    qDebug("Original time =  %s", str.toLocal8Bit().data());
    QTime TM2= TM1.addSecs(150);
    str = TM2.toString("HH:mm:ss");
    qDebug("150s later, time =  %s", str.toLocal8Bit().data());

    TM2 = QTime::currentTime();
    str = TM2.toString("HH:mm:ss zzz");
    qDebug("current time = %s",  str.toLocal8Bit().data());
    qDebug("Hour = %d", TM2.hour());
    qDebug("Minute = %d", TM2.minute());
    qDebug("Second = %d", TM2.second());
    qDebug("MSecond = %d", TM2.msec());
}

void Widget::on_btnDebugDate_clicked()
{
    QDate Date1(2025, 8, 1);
    QString str = Date1.toString("yyyy-MM-dd");
    qDebug("Original date =  %s", str.toLocal8Bit().data());

    QDate Date2;
    Date2.setDate(2025, 9, 1);
    str = Date2.toString("yyyy-MM-dd");
    qDebug("date 2 = %s", str.toLocal8Bit().data());
    qDebug("between date2 and date1 = %lld", Date2.daysTo(Date1));

    QDate curDate = QDate::currentDate();
    str = curDate.toString("yyyy-MM-dd");
    qDebug("current date = %s", str.toLocal8Bit().data());
    qDebug("year =  %d", curDate.year());
    qDebug("month = %d", curDate.month());
    qDebug("day = %d", curDate.day());
}

void Widget::on_btnDebugDateTime_clicked()
{
    QTime TM1(13, 24, 15);
    QDate Date1(2025, 8, 1);
    QDateTime dateTime1(Date1, TM1);
    QString str = dateTime1.toString("yyyy-MM-dd HH:mm:ss");
    qDebug("Original date time =  %s", str.toLocal8Bit().data());

    dateTime1 = QDateTime::currentDateTime();
    Date1 = dateTime1.date();
    str = Date1.toString("yyyy-MM-dd");
    qDebug("current date =  %s", str.toLocal8Bit().data());

    TM1 = dateTime1.time();
    str = TM1.toString("HH:mm:ss");
    qDebug("current time =  %s", str.toLocal8Bit().data());

    qlonglong time = dateTime1.toSecsSinceEpoch();
    qDebug("toSecsSinceEpoch() = %lld", time);

    time = time + 1000;
    qDebug("after 1000s,  toSecsSinceEpoch() = %lld", time);

    dateTime1.setSecsSinceEpoch(time);
    str = dateTime1.toString("yyyy-MM-dd HH:mm:ss");
    qDebug("Original date time =  %s", str.toLocal8Bit().data());
}

void Widget::on_btnGetTime_clicked()
{
    QDateTime dateTime = QDateTime::currentDateTime();
    QDate date = dateTime.date();
    QTime time = dateTime.time();
    ui->dateTimeEdit->setDateTime(dateTime);
    ui->dateEdit->setDate(date);
    ui->timeEdit->setTime(time);
    ui->calendarWidget->setSelectedDate(date);
    QString str;
    str = dateTime.toString("yyyy-MM-dd HH:mm:ss");
    ui->editDateTime->setText(str);
    str = date.toString("yyyy-MM-dd");
    ui->editDate->setText(str);
    str = time.toString("HH:mm:ss");
    ui->editTime->setText(str);
}

void Widget::on_btnSetTime_clicked()
{
    // QDateTime dateTime = QDateTime::fromString(ui->editDateTime->text());
    // ui->dateTimeEdit->setDateTime(dateTime);
    QTime time = QTime::fromString(ui->editTime->text());
    ui->timeEdit->setTime(time);
}

void Widget::on_btnSetDate_clicked()
{
    QDate date = QDate::fromString(ui->editDate->text());
    ui->dateEdit->setDate(date);
    ui->calendarWidget->setSelectedDate(date);
}

void Widget::on_btnSetDateTime_clicked()
{
    QDateTime datetime = QDateTime::fromString(ui->editDateTime->text());
    ui->dateTimeEdit->setDateTime(datetime);
    ui->calendarWidget->setSelectedDate(datetime.date());
}

void Widget::on_calendarWidget_clicked(const QDate &date)
{
    ui->editCalendar->setText(date.toString("yyyy-MM-dd"));
}

void Widget::on_timeEdit_timeChanged(const QTime &time)
{
    ui->editTime->setText(time.toString("HH:mm:ss"));
}

void Widget::on_dateEdit_dateChanged(const QDate &date)
{
    ui->editDate->setText(date.toString("yyyy-MM-dd"));
    ui->calendarWidget->setSelectedDate(date);
}

void Widget::on_dateTimeEdit_dateTimeChanged(const QDateTime &dateTime)
{
    ui->editDateTime->setText(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
    ui->calendarWidget->setSelectedDate(dateTime.date());
}

